package com.rightsidetech.scheduler.task;

import com.rightsidetech.activity.entity.MtActivity;
import com.rightsidetech.activity.entity.MtUserJoinActivityProcess;
import com.rightsidetech.activity.service.MtActivityAppService;
import com.rightsidetech.commom.enums.StoreActivityEnum;
import com.rightsidetech.system.dtos.response.SysStoreStorkRemindResp;
import com.rightsidetech.system.dtos.response.store.StoreStoreIdResp;
import com.rightsidetech.system.service.SysStoreStorkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: yhr
 * @Date: 2022-01-18 18:33
 */
@Slf4j
@Component
public class StockTimOutTask {
    @Resource
    private SysStoreStorkService sysStoreStorkService;
    @Resource
    private ScMassageTask massageTask;
    @Resource
    private MtActivityAppService mtActivityAppService;


    /**
     * 过期商品定时统计
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void outTimeStatis() {
        log.info("过期商品定时统计任务开始");
        try {

            sysStoreStorkService.outTimeStatis();

            log.info("过期商品定时统计任务结束");
        } catch (Exception e) {
            log.error("过期商品定时统计任务异常", e);
        }
    }


    @Scheduled(cron = "0 0 6 * * ?")
    public void mallDispatchOrder() {
        try {
            List<SysStoreStorkRemindResp> resps = sysStoreStorkService.storeStorkAgentLoginId();
            for (SysStoreStorkRemindResp agentIds:resps) {
                massageTask.agentPush(agentIds.getAgentId(),"门店库存存在有到期商品", (byte) 7,agentIds.getStoreId());
            }
            List<SysStoreStorkRemindResp> remindResps = sysStoreStorkService.storeStorkExceedAll();
            for (SysStoreStorkRemindResp resp:remindResps) {
                massageTask.storePush(resp.getStoreId(),"门店库存存在有到期商品",(byte) 7);
//                sysStoreStorkService.updateRemind(resp.getDetailsId());
            }
        } catch (Exception e) {
            log.error("库存过期提醒任务异常", e);
        }
    }


    @Scheduled(cron = "0 0 1 * * ?")
    public void activityStatus(){
        try {
            List<MtActivity> list = mtActivityAppService.selectHeadList(StoreActivityEnum.HJ003.getDicCode());
            for (MtActivity activity : list) {
                List<MtUserJoinActivityProcess> processList = mtActivityAppService.selectProcessList(activity.getId());
                for (MtUserJoinActivityProcess process : processList) {
                    mtActivityAppService.updateUserJoin(activity.getId(),process.getUserAccountId(),(short)-1);
                }
            }
        }catch (Exception e){
            log.error("堆头活动修改状态出现异常:{}",e);
        }
    }


}
